<?php

namespace app\common\controller;


class Sys
{


    //获取服务器信息 - 2020-6-19
    //*+------------------------------------------------------------------------+*/


    //获取系统信息 @author leiqianyong 2020-6-19 16:55 PM
    public function sysinfo(){
        $info = array(
            '服务器域名/IP:端口'=>$_SERVER['SERVER_NAME'].' [ '.$_SERVER['SERVER_ADDR'].'：'.$_SERVER['SERVER_PORT'].' ]',
            '操作系统'=>PHP_OS,
            'Web运行环境'=>$_SERVER["SERVER_SOFTWARE"],
            'PHP版本'=>PHP_VERSION,
            'PHP运行方式'=>php_sapi_name(),
            'MySQL 版本'=>$this->_mysql_version(),
            '数据库大小'=>$this->_mysql_db_size(),
            '上传附件限制'=>ini_get('upload_max_filesize'),
            '执行时间限制'=>ini_get('max_execution_time').'秒',
            '当前磁盘剩余空间'=>byte_format(disk_free_space("."),1),
            '当前磁盘总空间'=>byte_format(disk_total_space("."),1),
            '系统类型版本'=>php_uname(),
            '服务器语言'=>$_SERVER['HTTP_ACCEPT_LANGUAGE'],

            //'register_globals'=>get_cfg_var("register_globals")=="1" ? "ON" : "OFF",
            //'magic_quotes_gpc'=>(1===get_magic_quotes_gpc())?'YES':'NO',
            //'magic_quotes_runtime'=>(1===get_magic_quotes_runtime())?'YES':'NO',
        );

        $html="<table>";
        foreach($info as $key=>$vo){
            $html.="<tr>";
            $html.="<td style='width:200px;color:#ff0000;'>".$key."</td><td>".$vo."</td></tr>";
            $html.="</tr>";
        }
        $html.="</table>";
        echo $html;
    }

    protected static function _model(){
        return db();
    }

    private function _mysql_version(){
        $Model = self::_model();
        $version = $Model->query("select version() as ver");
        return $version[0]['ver'];
    }

    private function _mysql_db_size()
    {
        $Model = self::_model();
        $sql = "SHOW TABLE STATUS FROM ".config('database.database');
        $tblPrefix = config('database.prefix');
        if($tblPrefix != null) {
            $sql .= " LIKE '{$tblPrefix}%'";
        }

        $row = $Model->query($sql);
        $size = 0;
        foreach($row as $value) {
            $size += $value["Data_length"] + $value["Index_length"];
        }
        return byte_format($size);

        //方式2
        /**
        $db=C('DB_NAME');
        $sql = 'SELECT sum(data_length + index_length )/1024/1024 as datasize FROM information_schema.TABLES where table_schema="' . $db. '" GROUP BY table_schema';
        $res=M()->query($sql);
        if(empty($res))return 0;
        return number_format($res[0]['datasize'], 2).'M';
         ***/
    }

}